﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace ServeNHL.Models
{
    [MetadataType(typeof(userMetadata))]
    public partial class USER
    {

        public int SaveUser(UserViewModel user)
        {
            int userID = 0;
            Boolean isUsernameAvailable = isAvailableUsername(user.user.USERNAME);

            int temp;
            if (user.user.USER_ID != 0 && (Int32.TryParse(user.user.USER_ID.ToString(), out temp)))
            {
                UpdateUser(user.user);
            }
            else
            {
                InsertUser(user.user);
            }

            userID = user.user.USER_ID;
            return userID;
        }

        /// <summary>
        /// Adds a given user to the user database table
        /// </summary>
        /// <param name="user">User to add to database</param>
        /// <returns>User ID</returns>
        public int InsertUser(USER user)
        {
            int userID = 0;

            using (var dbconn = new fclick_dbEntities())
            {
                var curUser = new USER
                {
                    USERNAME = user.USERNAME,
                    PASSWORD = Helpers.UserHelper.getPasswordSaltHash(user.PASSWORD),
                    STATUS = user.STATUS,
                    CREATED_DATE = Helpers.createDate.getCreateDate(),
                    LAST_MOD_DATE = Helpers.createDate.getCreateDate(),
                    FAMILY_ID = user.FAMILY_ID,
                };

                dbconn.USERs.Add(curUser);
                dbconn.SaveChanges();

                userID = curUser.USER_ID;
            }

            return userID;
        } // End of public int InsertUser(USER user)

        
        /// <summary>
        /// Updates a user object in the database
        /// </summary>
        /// <param name="user">User object to update</param>
        /// <returns>User ID</returns>
        public int UpdateUser(USER user)
        {
            int userID = 0;

            using (var dbconn = new fclick_dbEntities())
            {

                var tempUser = new USER();

                if (!String.IsNullOrEmpty(user.USERNAME))
                {
                    tempUser.USERNAME = user.USERNAME;
                }

                if (!String.IsNullOrEmpty(user.PASSWORD))
                {
                    tempUser.PASSWORD = user.PASSWORD;
                }

                if (!String.IsNullOrEmpty(user.STATUS))
                {
                    tempUser.STATUS = user.STATUS;
                }
                if (user.FAMILY_ID > 0)
                {
                    tempUser.FAMILY_ID = user.FAMILY_ID;
                }

                tempUser.CREATED_DATE = DateTime.Now;
                tempUser.LAST_MOD_DATE = DateTime.Now;

                dbconn.USERs.Add(tempUser);
                dbconn.SaveChanges();

                userID = tempUser.USER_ID;
            }
            return userID;
        } //End of public int UpdateUser(USER user) 

        /*
        public int UpdateUser(USER user)
        {
            int ID = 0;

            using (var dbconn = new fclick_dbEntities())
            {
                var u = (from us in dbconn.USERs
                         where us.USER_ID == user.USER_ID
                         select us).FirstOrDefault();

                u.USERNAME = user.USERNAME;
                u.PASSWORD = user.PASSWORD;
                u.STATUS = user.STATUS;
                u.CREATED_DATE = user.CREATED_DATE;
                u.LAST_MOD_DATE = user.LAST_MOD_DATE;
                u.FAMILY_ID = user.FAMILY_ID;
            }

            return ID;
        } */

        /// <summary>
        /// Checks if a given username is already used in the user's table.
        /// </summary>
        /// <param name="usrname">Username to check</param>
        /// <returns>False if the username is already in use.</returns>
        public Boolean isAvailableUsername(string usrname)
        {
            Boolean isValid = false;

            using (var dbconn = new fclick_dbEntities())
            {
                var count = dbconn.USERs.Count(u => u.USERNAME == usrname);
                if (count == 0)
                {
                    isValid = true;
                }
                else
                {
                    isValid = false;
                }
            }
            return isValid;
        } // public Boolean isAvailableUsername(string usrname)
        /// <summary>
        /// gets user object by id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public USER getUser (int id)
        {
            USER user = new USER();
            using(var db = new fclick_dbEntities())
            {
                var q = db.USERs.Where(u => u.USER_ID == id);
                user = q.FirstOrDefault();

            }
            return user;
        }// end of getuser
    }
}